home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2296 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  76 lines

  1. Path: news.microsoft.com!news
  2. From: warlok@siu.edu (Jon Fincher)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with c code, please help!
  5. Date: 19 Jan 1996 22:57:30 GMT
  6. Organization: Microsoft Corp.
  7. Message-ID: <4dp7kq$ohm@news.microsoft.com>
  8. References: <surgsw-1901960148530001@128.206.206.86>
  9. NNTP-Posting-Host: 157.54.53.180
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In article <surgsw-1901960148530001@128.206.206.86>, 
  15. surgsw@mizzou1.missouri.edu says...
  16. >
  17. >I have been trying to get this very simple piece of code to work for
  18. >hours.  What is the problem???????
  19. >
  20. >#include <stdio.h>
  21. >
  22. >main()
  23. >{
  24. >   int i=0;
  25. >   char  word[100], c;
  26. >   printf("Enter a word:   ");
  27. >   while( (c = getchar()) != '\n')  {
  28. >      *word = c;
  29. >      word == word + 1;
  30. >   }  
  31. >   printf("You entered:  ");  
  32. >   *word = 0;
  33. >   while( *word != '\n' )   
  34. >   {  
  35. >      putchar( *word );
  36. >      word == word + 1; 
  37. >   }
  38. >}  
  39.  
  40. Well, the == is not assignment, it's comparitive equivalence.  the = assigns 
  41. one value to a variable, the == compares the two sides and returns TRUE or 
  42. FALSE (NOT 0 or 0).
  43.  
  44. Also, you have declared word to be a constant array, i.e. you dimensioned it 
  45. as a declaration.  You'll never be able to do pointer arithmetic on it.  To do 
  46. what you want, try this....
  47.  
  48. #include <stdio.h>
  49.  
  50. main()
  51. {
  52.    int i=0;
  53.    char  word[100], c;
  54.    char *w;            //NEW LINE!!!!
  55.    w = word;            //NEW LINE!!!!
  56.    printf("Enter a word:   ");
  57.    while( (c = getchar()) != '\n')  {
  58.       *w = c;            //CHANGED
  59.       w++;            //CHANGED
  60.    }  
  61.    printf("You entered:  ");  
  62.    *w = 0;            //CHANGED
  63.    while( *w != '\n' )       //CHANGED
  64.    {  
  65.       putchar( *w );        //CHANGED
  66.       w++;             //CHANGED
  67.    }
  68. }  
  69.  
  70. Note in this example I created a char * to walk through the array.  You could 
  71. have easily done this using an integer array index and incrementing it rather 
  72. than a walking pointer.
  73.  
  74. Jon
  75.  
  76.